﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeakOut.Lib;

namespace SpeakOut.Model
{
    [Serializable]
    public class Post
    {
        public virtual Guid PostId { get; set; }

        public virtual string Title { get; set; }

        public virtual string Body { get; set; }

        public virtual string Permalink { get; set; }

        public virtual bool Published { get; set; }

        public virtual DateTime? PublishDate { get; set; }

        public virtual Author Author { get; set; }

        [System.Xml.Serialization.XmlIgnore]
        public virtual bool Visible
        {
            get
            {
                return Published && PublishDate.HasValue && PublishDate.Value <= DateTime.Now;
            }
        }

        private IList<Comment> _comments;
        public virtual IEnumerable<Comment> Comments
        {
            get { return _comments; }
        }

        private IList<Tag> _tags;
        public virtual IEnumerable<Tag> Tags
        {
            get { return _tags; }
        }

        private IList<Category> _categories;
        public virtual IEnumerable<Category> Categories
        {
            get { return _categories; }
        }

        public Post()
        {
            PostId = Guid.Empty;
            _tags = new List<Tag>();
            _categories = new List<Category>();
            _comments = new List<Comment>();
        }

        public virtual Post AddComment(Comment comment)
        {
            comment.Post = this;
            _comments.Add(comment);
            return this;
        }

        public virtual Post AddTag(Tag tag)
        {
            tag.Post = this;
            _tags.Add(tag);
            return this;
        }

        public virtual bool BelongsToCategory(Guid categoryId)
        {
            return Categories.HasElement(x => x.CategoryId.Equals(categoryId));
        }

        public virtual Post RemoveAllTags()
        {
            _tags.Clear();
            return this;
        }

        public virtual Post RemoveAllCategories()
        {
            _categories.Clear();
            return this;
        }

        public virtual Post AddCategories(IEnumerable<Category> categories)
        {
            foreach (var category in categories)
            {
                _categories.Add(category);
            }
            return this;
        }

        public override string ToString()
        {
            return string.Format("{0} # Comments:{1} # Categories:{2}", Title, Comments.Count(), Categories.Count());
        }

        private bool Equals(Post other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.PostId.Equals(PostId);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (Post)) return false;
            return Equals((Post) obj);
        }

        public override int GetHashCode()
        {
            return PostId.GetHashCode();
        }
    }
}
